package fr.umlv.search;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.MouseEvent;

import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.event.ChangeEvent;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumnModel;

import fr.umlv.cmis.listview.CmisObjectListViewModel;
import fr.umlv.cmis.listview.DetailModifiedCellRenderer;
import fr.umlv.cmis.listview.DetailNameCellEditor;
import fr.umlv.cmis.listview.DetailSizeCellRenderer;

// TODO: Auto-generated Javadoc
/**
 * The Class DetailViewSearch.
 */
public class DetailViewSearch extends JTable{
	
/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;
	
	/** The do not edit. */
	private boolean doNotEdit = false;
    
    /**
     * Instantiates a new detail view search.
     *
     * @param searchModel the search model
     */
    @SuppressWarnings("serial")
	public DetailViewSearch(SearchModel searchModel) {
        super(searchModel);
                
        JTableHeader header = getTableHeader();
        header.setReorderingAllowed(true);
        header.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                onHeaderMouseClicked(e);
            }
            
        });
        
        
        columnModel.getColumn(0).setPreferredWidth(200);
        columnModel.getColumn(1).setPreferredWidth(20);
        columnModel.getColumn(2).setPreferredWidth(50);
        columnModel.getColumn(3).setPreferredWidth(50);
        columnModel.getColumn(4).setPreferredWidth(100);
        columnModel.getColumn(5).setMaxWidth(20);
        
        columnModel.getColumn(0).setCellRenderer(new SearchCellRenderer());
        columnModel.getColumn(1).setCellRenderer(new DetailSizeCellRenderer());
        columnModel.getColumn(3).setCellRenderer(new DetailModifiedCellRenderer());
        columnModel.getColumn(4).setCellRenderer(new DefaultTableCellRenderer(){
        	
        	public Component getTableCellRendererComponent(JTable table, Object
        			value, boolean isSelected, boolean hasFocus, int row, int column) {
        			           JLabel button = new JLabel((String)value);
        			           button.setSize(new Dimension(5,5));
        			           button.setToolTipText((String) value);
        			           return button;
        			    }
        });
        columnModel.getColumn(5).setCellRenderer(new LocationCellRenderer());
        
        // set column editor
        DetailNameCellEditor newCellEditor = new DetailNameCellEditor();
        columnModel.getColumn(0).setCellEditor(newCellEditor);
        
    }
    
    /**
     * Gets the view model.
     *
     * @return the view model
     */
    public CmisObjectListViewModel getViewModel() {
        return (CmisObjectListViewModel)getModel();
    }
    
    /* (non-Javadoc)
     * @see javax.swing.JTable#isCellEditable(int, int)
     */
    @Override
    public boolean isCellEditable(int row, int col) {
        boolean b = false;
        //ShellListViewModel dataModel = (ShellListViewModel)getModel();
        //if ((col == 0) && (dataModel.getState() == ShellListViewModel.EDIT)) {
        if ((col == 0)  && (doNotEdit == false)) {
            b = true;
        }
        doNotEdit = false;
        return b;
    }
    
    /* (non-Javadoc)
     * @see javax.swing.JComponent#processMouseEvent(java.awt.event.MouseEvent)
     */
    @Override
    protected void processMouseEvent(MouseEvent evt) {
        if (evt.getClickCount() == 2) {
            doNotEdit = true;
        }
        super.processMouseEvent(evt);
    }
    
    /* (non-Javadoc)
     * @see javax.swing.JTable#editingCanceled(javax.swing.event.ChangeEvent)
     */
    @Override
    public void editingCanceled(ChangeEvent evt) {
    	CmisObjectListViewModel model = (CmisObjectListViewModel)getModel();
        model.setState(CmisObjectListViewModel.BROWSE);
        super.editingCanceled(evt);
    }
    
    /* (non-Javadoc)
     * @see javax.swing.JTable#editingStopped(javax.swing.event.ChangeEvent)
     */
    @Override
    public void editingStopped(ChangeEvent evt) {
    	CmisObjectListViewModel modle = (CmisObjectListViewModel)getModel();
        modle.setState(CmisObjectListViewModel.BROWSE);
        super.editingStopped(evt);
        
    }
    
    /**
     * On header mouse clicked.
     *
     * @param evt the evt
     */
    private void onHeaderMouseClicked(MouseEvent evt) {
        TableColumnModel colModel = getColumnModel();
        int viewColumnIndex = colModel.getColumnIndexAtX(evt.getX());
        int column = convertColumnIndexToModel(viewColumnIndex);
        
        if (evt.getClickCount() == 1 && column != -1) {
        	SearchModel model = (SearchModel)getModel();
            model.setSortBy(column);
            model.refresh();
        }
    }

}